1861C - Queries for the Array - CodeForces Solution


data structures dfs and similar strings trees

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const ll MOD = 998244353;
void solve() {
    string s; cin >> s;
    int sorted = 0, len = 0, state = 1, pos = 0,nxt=0;
    // for(;pos<s.size();pos++) {
    //     if(s[pos]=='+' || s[pos]=='-') break;
    //     if(s[pos]=='1') continue;
    //     if(s[pos]=='0') {
    //         cout<<"NO"<<endl;
    //         return;
    //     }
    // }
    // for (;pos < s.size();pos++) {
    //     if (s[pos] == '+') {
    //         len++;
    //     } else if (s[pos] == '-') {
    //         len--;
    //     } else if (s[pos] == '0') {
    //         state=0;
    //         break;
    //     } else {
    //         state=1;
    //         sorted=len;
    //         break;
    //     }
    // }
    // pos++;
    for (;pos < s.size();pos++) {
        if (s[pos] == '+') {
            len++;
            if (len < 2) {
                sorted = len;
                // state=1;
            }
            if(state) nxt++;
        } else if (s[pos] == '-') {
            if (len == nxt) state = 1;
            len--;
            sorted = min(sorted, len);
            if(state) nxt=min(nxt,len);    
        } else if (s[pos] == '0') {
            if (sorted == len) {
                cout << "NO" << endl;
                return;
            }
            state = 0;
            nxt=min(nxt,len);
        } else {
            if (len>=nxt && !state) {
                // cerr<<nxt<<endl;
                cout << "NO" << endl;
                return;
            }
            state = 1;
            sorted = len;
            nxt=sorted;
        }
    }
    // cerr<<sorted<<" "<<len<<endl;
    cout << "YES" << endl;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    cin >> t;
    while (t--) solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1705A - Mark the Photographer
1707A - Doremy's IQ
1706B - Making Towers
1325B - CopyCopyCopyCopyCopy
1649C - Weird Sum
1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces
711A - Bus to Udayland
779B - Weird Rounding
1703D - Double Strings
1704C - Virus
63A - Sinking Ship
1704B - Luke is a Foodie
298B - Sail
239A - Two Bags of Potatoes
1704E - Count Seconds
682A - Alyona and Numbers
44A - Indian Summer
1133C - Balanced Team
1704A - Two 0-1 Sequences
1467A - Wizard of Orz
1714E - Add Modulo 10
1714A - Everyone Loves to Sleep
764A - Taymyr is calling you
1714B - Remove Prefix
1264F - Beautiful Fibonacci Problem